package com.ly.mpayment.rest.system;


import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import com.ly.mpayment.core.util.UserUtil;
import com.ly.mpayment.persist.entity.UpShopProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.persist.entity.DownShopProduct;
import com.ly.mpayment.persist.entity.MatchingStrategy;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.downshopproduct.IDownShopProductService;
import com.ly.mpayment.persist.service.matchingstrategy.IMatchingStrategyService;
import com.ly.mpayment.persist.service.upshoppro.IUpShopProService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Component
@Path("system/productstrategy/")
public class ProductStrategyRest extends AbsRest{

	@Autowired
	IUserService userService;

	@Autowired
	IDownShopProductService downService;

	@Autowired
	IMatchingStrategyService strategyService;

	@Autowired
	IUpShopProService upProductService;
	@Autowired
	IDownShopProductService productService;
	/**
	 * 添加产品匹配策略
	 * 按运营商 省份 业务类型匹配
	 * @param data
	 * @param request
	 * @return
	 */
	@Path("addProductStrategy")
	@POST
	public String addProductStrategy(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "downuuid","remark","upuuid","provinceuuid","operatoruid",
				"cityName","busuuid","upprouuids","downprouuid");
		if(result == null){

			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
						if (jsonToken != null) {
							User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
							if(login != null && UserUtil.checkRole(login)){
								JSONObject json = JsonUtils.converToJson(data);
								return  strategyService.addMatchingstrategy(json,login);
							}
						}
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	@Path("queryStrategyPage")
	@POST
	@SuppressWarnings("static-access")
	public String queryStrategyPage(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"start","pageSize");
		if(result == null){
			JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
						if (jsonToken != null) {

							User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
							if (login != null && UserUtil.checkRole(login)) {
								return ResultMessage.getSucceed( this.strategyService.queryStrategyPage(json, json.getInt("start"),
										json.getInt("pageSize")));
							}
						}
					}
				}catch (Exception e){
				}
			}
		}
		return result;
	}

	@Path("updateStrategy")
	@POST
	@SuppressWarnings("static-access")
	public String updateStrategy(String data,@Context HttpServletRequest request){
		//orderSort
		String result = this.validationParameterGetRole( request, userService);
		if(result == null){
			JSONObject json = JsonUtils.getJSONOBJCT().fromObject(data);
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
						if (jsonToken != null) {
							User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
							if (login != null && UserUtil.checkRole(login)) {
								if(this.strategyService.updateStrategy(json)){
									return ResultMessage.getSucceed( );
								}
								return ResultMessage.getFailt("");
							}
						}
					}
				}catch (Exception e){
				}
			}
		}
		return result;
	}

	/**
	 * 批量定时任务
	 * 按运营商 省份 业务类型匹配
	 * @param data
	 * @param request
	 * @return
	 */
	@Path("batchProductTimer")
	@POST
	public String batchProductTimer(String data,@Context HttpServletRequest request){
		String result = this.validationParameterGetRole(request, userService);
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
						if (jsonToken != null) {
							User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
							if (login != null && UserUtil.checkRole(login)) {
								return strategyService.batchProductTimer(json,login);
							}
							return ResultMessage.getFailt("权限不足");
						}
					}
				}catch (Exception e){
					e.printStackTrace();
				}
			}

		}
		return result;
	}

	/**
	 * 批量定时任务上游
	 * @param data
	 * @param request
	 * @return
	 */
	@Path("batchProductTimerUp")
	@POST
	public String batchProductTimerUp(String data,@Context HttpServletRequest request){
		String result = this.validationParameterGetRole(request, userService);
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			String token = request.getHeader("token");
			if (token != null) {
				DecodedJWT jwt;
				try {
					jwt = JWT.decode(token);
					if (jwt != null) {
						JSONObject jsonToken = JsonUtils.converToJson(jwt.getSubject());
						if (jsonToken != null) {
							User login = userService.findByUserid(jsonToken.getString(Config.tokenSignAttr));
							if (login != null && UserUtil.checkRole(login)) {
								return strategyService.batchProductTimerUp(json,login);
							}
							return ResultMessage.getFailt("权限不足");
						}
					}
				}catch (Exception e){
					e.printStackTrace();
				}
			}

		}
		return result;
	}
}
